package aishua.com.mytestapplist.engin.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import aishua.com.mytestapplist.engin.MyTrustManager;
import aishua.com.mytestapplist.engin.TrustAnyHostnameVerifier;
import aishua.com.mytestapplist.engin.WebService;
import aishua.com.mytestapplist.utils.CTWalletLog;


public abstract class WebServiceImpl implements WebService {
	private static final int REQUEST_TIME_OUT = 180000;
	private static final int READ_TIME_OUT = 1800000;
	@Override
	public InputStream executeHttpsGet(String host)throws NoSuchAlgorithmException, KeyManagementException, Exception {
		HttpsURLConnection conn = getHttps(host);
		conn.setRequestMethod("GET");
		// »ñÈ¡ÏÂ·¢ÇëÇó
		InputStream inputStream = conn.getInputStream();
		return inputStream;
	}
	
	public InputStream executeHttpGet(String host) throws IOException {
		HttpURLConnection conn = getHttp(host);
		conn.setRequestMethod("GET");
		// »ñÈ¡ÏÂ·¢ÇëÇó
		InputStream inputStream = conn.getInputStream();
		return inputStream;
	}

	public HttpsURLConnection getHttps(String host) throws NoSuchAlgorithmException, KeyManagementException, IOException {
		SSLContext context = SSLContext.getInstance("TLS");
		context.init(null, new TrustManager[]{new MyTrustManager()}, null);
		URL url = new URL(host);
		HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
		conn.setSSLSocketFactory(context.getSocketFactory());
		conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
		
		conn.setDoInput(true);
		conn.setDoOutput(true);
		
	    conn.setConnectTimeout(REQUEST_TIME_OUT);  
	    conn.setReadTimeout(READ_TIME_OUT); 
	    return conn;
	}
	public HttpURLConnection getHttp(String host) throws IOException {
		URL url = new URL(host);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoInput(true);
		conn.setDoOutput(true);
	    conn.setConnectTimeout(REQUEST_TIME_OUT);  
	    conn.setReadTimeout(READ_TIME_OUT); 
	    return conn;
	}
	@Override
	public InputStream executeHttpsPost(String host, String params) throws NoSuchAlgorithmException, KeyManagementException, IOException {
		if(null==params||null==host||"".equals(params)){
			CTWalletLog.printW("executeHttpsPost--->RuntimeException null params");
			throw new RuntimeException("null params");
		}
		HttpsURLConnection conn = getHttps(host);
	    conn.setRequestMethod("POST");
		
		OutputStream outStream = conn.getOutputStream();
		outStream.write(params.getBytes());
		outStream.flush();
		outStream.close();
		InputStream inStream = conn.getInputStream();
		return inStream;
	}
	@Override
	public String executeHttpsPost(String host, String params, boolean isGetString) throws KeyManagementException, NoSuchAlgorithmException, IOException {
		String result = null;
		if(isGetString){
			InputStream inStream = executeHttpsPost(host,params);
			int len = 0;
			byte[] buf = new byte[1024];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while((len=inStream.read(buf))!=-1){
				baos.write(buf, 0, len);
			}
			result = baos.toString();
			baos.close();
			inStream.close();
			return result;
		}else{
			throw new RuntimeException("isGetString==true");
		}
	}

	public abstract <Result,Param> Result executePost(Param v,Class<Result> clazz);

	public abstract<Result,Param,Inside> Result exectePost(Param v, Class<Result> outSideClazz, Class<Inside> inSideClazz);
}
